//
//  RMMainCenterView.m
//  RMNote
//
//  Created by lxf on 16/4/21.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMMainCenterView.h"
#import "RMTimeHandle.h"
#import "RMDateModel.h"
#import "RMDateTableView.h"
#import "RMNoteTableView.h"
#import "RMAddImageView.h"
#import "RMDateBaseManager.h"
#import "RMBaseModel.h"
#import "NCAnimationView.h"
#define kDateTableViewWith 96

@interface RMMainCenterView ()


@property (nonatomic, strong) RMDateModel *model;
@property (nonatomic, strong) RMDateBaseManager *manager;

@end


@implementation RMMainCenterView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createAllView];
    }
    return self;
}

- (void)createAllView{
    // 初始化数据库工具对象
    self.manager = [RMDateBaseManager defaultManager];

    
    // 1. 给 RMDateTableView 获取数据
    [self updateModel];
    [self creatDateTabelView];
    
    // 2. 给 RMNoteTableView 获取数据
    [self getDataForNoteTableView];
    [self createNoteTableView];
    

}


// 给 RMDateTableView获取数据
- (void)updateModel{
    NSArray *dateArr =[self.manager selectCreateDateFromTable:kRMNoteTable];
    
    self.model = [[RMDateModel alloc]initWithDateArr:dateArr];
}

//  生成 RMDateTableView
- (void)creatDateTabelView{
    self.dateTableView = nil;
    self.dateTableView = [[RMDateTableView alloc]initWithFrame:CGRectMake(0, 30, kDateTableViewWith, 400) style:(UITableViewStylePlain) andDateModel:self.model];
    [self addSubview:self.dateTableView];

    
}

// 给 RMNoteTableView获取数据
- (void)getDataForNoteTableView{
}

// 生成 RMNoteTableView
- (void)createNoteTableView{
    self.noteTableView = nil;
    self.noteTableView = [[RMNoteTableView alloc]initWithFrame:CGRectMake(kDateTableViewWith, 0, kScreenWidth - self.dateTableView.frame.size.width, kScreenHeight - kTopHeight) style:(UITableViewStylePlain) model:self.model];
    __weak RMMainCenterView *cv = self;
    self.noteTableView.selectCellBlock = ^(RMBaseModel *model){
        cv.pushNoteViewBlock(model);
    };
    [self addSubview:self.noteTableView];

}

- (void)createAddImageView{
    UIImage *image = [UIImage imageNamed:@"2.jpg"];
    UIImage *btImage = [UIImage imageNamed:@"delet"];
    RMAddImageView *addImage = [[RMAddImageView alloc]initWithFrame:CGRectMake(0, 300, kScreenWidth, 250) withPhotoImage:image andBtnImage:btImage];
    [self addSubview:addImage];
    
}


- (void)reloadDataForNoteTableView{
    [self updateModel];
    [self createNoteTableView];
}

- (void)reloadDataForDateTableView{
    [self updateModel];
    [self creatDateTabelView];
}



#pragma mark - 懒加载

//  模拟数据插入数据库
- (NSArray *)createData{
    
    [self.manager createTable:kRMNoteTable];
    NSMutableArray *arr = [NSMutableArray array];
    
    for (int i = 0; i < 100; i++) {
        int year = arc4random_uniform(6)+2010;
        int moth = arc4random_uniform(12)+1;
        int day = arc4random_uniform(27)+1;
        int hour = arc4random_uniform(23)+1;
        int minute = arc4random_uniform(59)+1;
        int second = arc4random_uniform(59) + 1;

        NSString *str = [NSString stringWithFormat:@"%d%02d%02d%02d%02d%02d",year,moth,day,hour,minute,second];
        
        NSDate *date = [[RMTimeHandle shareInstance] dateForDateStr:str andFormatter:@"yyyyMMddHHmmss"];
        RMBaseModel *model = [[RMBaseModel alloc]init];
        model.content = [NSString stringWithFormat:@"%d-%d-%d %d:%d:%d\n%@",year,moth,day,hour,minute,second,date];
        model.title = @"如果~有如果";
        model.createDate = date;
        
        BOOL result = [self.manager insertIntoTable:kRMNoteTable createDate:date Model:model];
    }
    
    return arr;
}




@end
